package com.gviiii.baiduai.file.monitor;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FileMonitor {

    private ScheduledExecutorService scheduledExecutorService;

    public FileMonitor() {
    }

    public FileMonitor(String prefix){
        ThreadFactoryBuilder tfb=new ThreadFactoryBuilder();
        tfb.setDaemon(false);
        tfb.setNameFormat(prefix);
        this.scheduledExecutorService=Executors.newScheduledThreadPool(1,tfb.build());
    }

    public ScheduledExecutorService getScheduledExecutorService() {
        return scheduledExecutorService;
    }

    public void scheduleAtFixedRate(Runnable runnable,int delay, int period, TimeUnit unit){
        scheduledExecutorService.scheduleAtFixedRate(runnable,delay,period,unit);
    }

    public void schedule(Runnable runnable,int delay, TimeUnit unit){
        scheduledExecutorService.schedule(runnable,delay,TimeUnit.SECONDS);
    }
}
